package com.leoncom.sms_silencer.util;

import java.util.Calendar;

import android.content.Context;
import android.util.Log;

public class SilencerTask {
	public static int DaysPerWeek = 7;

	private long id;
	private int hour;
	private int min;
	private int period;
	private boolean init;
	private boolean[] repeatedDays = new boolean[DaysPerWeek];

	public SilencerTask() {
		this.id = 0;
		this.hour = 0;
		this.min = 0;
		this.period = 0;
		this.init = false;
		for (int i = 0; i < DaysPerWeek; i++) {
			this.repeatedDays[i] = false;
		}
	}

	public boolean isInit() {
		return init;
	}

	public void setInit(boolean init) {
		this.init = init;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getHour() {
		return hour;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public int getMin() {
		return min;
	}

	public void setMin(int min) {
		this.min = min;
	}

	public int getPeriod() {
		return period;
	}

	public void setPeriod(int period) {
		this.period = period;
	}

	public boolean[] getRepeatedDays() {
		return this.repeatedDays;
	}

	public void setRepeatedDay(int index) {
		if (index > 0 && index < DaysPerWeek) {
			this.repeatedDays[index] = true;
		}
	}

	public void unsetRepeatedDay(int index) {
		if (index > 0 && index < DaysPerWeek) {
			this.repeatedDays[index] = false;
		}
	}

	public String concatDaysIndexToString() {
		String result = "";
		for (int i = 0; i < DaysPerWeek; i++) {
			if (this.repeatedDays[i] == true) {
				result += "1";
			} else {
				result += "0";
			}

			if (i != DaysPerWeek - 1) {
				result += ",";
			}
		}

		return result;
	}

	public String concatPlainDayString(Context ctxt) {
		final CharSequence[] weekDayItems = CommonUtils.getDayOfWeekNames(ctxt,
				Calendar.SHORT);
		String result = "";
		boolean init_once = false;
		for (int i = 0; i < DaysPerWeek; i++) {
			if (this.repeatedDays[i] == true) {
				if (init_once == true) {
					result += ",";
				}
				result += weekDayItems[i];
				init_once = true;
			}
		}

		return result;
	}

	public void initDaysIndexFromString(String input) {
		String[] strArray = input.split(",");
		if (strArray.length != DaysPerWeek) {
			return;
		}
		for (int i = 0; i < DaysPerWeek; i++) {
			int intValue = Integer.valueOf(strArray[i]);
			if (intValue == 1) {
				this.repeatedDays[i] = true;
			} else {
				this.repeatedDays[i] = false;
			}
		}
	}

	public boolean isMuteNow() {
		if (this.init == false) {
			return false;
		}

		Calendar now = Calendar.getInstance();
		for (int i = 0; i < DaysPerWeek; i++) {
			if (this.repeatedDays[i] == false) {
				continue;
			}
			
			Calendar start = Calendar.getInstance();
			start.set(Calendar.DAY_OF_WEEK, i + 1);
			start.set(Calendar.HOUR_OF_DAY, this.hour);
			start.set(Calendar.MINUTE, this.min);

			Calendar end = (Calendar) start.clone();
			end.add(Calendar.MINUTE, this.period);

			Log.i("Time Compare", "Start : " + String.valueOf(start.getTimeInMillis() / 1000) +
								  "Now : " + String.valueOf(now.getTimeInMillis() / 1000) + 
								  "End : " + String.valueOf(end.getTimeInMillis() / 1000));
			// now belongs to (start,end)
			if (start.before(now) && end.after(now)) {
				return true;
			}
		}
		return false;
	}
}
